<template>
  <el-dialog :visible.sync="value" :before-close="cancel" append-to-body width="60%">
    <el-form
      class="form-container"
      :model="form"
      :rules="rules"
      ref="refForm"
      label-width="100px"
      style="max-height:70vh; overflow:auto;padding:10px;"
    >
      <div class="box">
        <div class="createPost-container">
          <el-row>
            <el-col>
              <div class="grid-content bg-purple">{{this.message.receiptTime|toTime}}</div>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <div class="grid-content bg-purple">
                <span class="inbox-common-title">账户:</span>
                {{this.message.userName}}
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <div class="grid-content bg-purple">
                <span class="inbox-common-title">类别:</span>
                {{this.message.typeName}}
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <div class="grid-content bg-purple">
                <span class="inbox-common-title">标题:</span>
                {{this.message.title}}
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <div class="grid-content bg-purple">
                <span class="inbox-common-title">描述:</span>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col>{{this.message.content}}</el-col>
          </el-row>
        </div>
      </div>
      <el-form-item label="发送至:">
        <el-input
          type="textarea"
          :rows="1"
          class="article-textarea"
          autosize
          :disabled="true"
          v-model="message.userName"
        ></el-input>
      </el-form-item>

      <el-form-item label="标题:">
        <el-input
          type="textarea"
          :rows="1"
          :disabled="true"
          class="article-textarea"
          autosize
          v-model="message.title"
        ></el-input>
      </el-form-item>

      <el-form-item label="内容:" prop="content">
        <Tinymce :height="400" ref="editor" v-model="form.content"/>
        <el-button @click="handlePcPreview">预览PC效果</el-button>
        <el-button @click="handleMobilePreview">预览APP效果</el-button>
        <pc-preview
          v-show="preview.pc"
          :name="message.title"
          :content="form.content"
          :time="form.sendTime"
        />
        <mobile-preview
          v-show="preview.moblie"
          :name="message.title"
          :content="form.content"
          :time="form.sendTime"
        />
      </el-form-item>
      <el-row>
        <el-button
          v-loading="loading"
          style="margin: 10px;float:right"
          :disabled="isDisabled"
          type="primary"
          @click="doSave({
                 action: 'immediately',
                 form: {
                 name: 'refForm',
                 scrollToError: true
                 }})"
        >立即发送</el-button>
      </el-row>
      <el-row>
        <el-form-item label="发送时间:" class="postInfo-container-item" prop="sendTime">
          <el-date-picker
            v-model="form.sendTime"
            class="filter-item"
            start-placeholder="开始日期"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            align="center"
          ></el-date-picker>
          <el-button
            v-loading="loading"
            style="margin:0 10px;float:right"
            :disabled="isDisabled"
            type="primary"
            @click="doSave({
                 action: 'delay',
                 form: {
                 name: 'refForm',
                 scrollToError: true
                 }})"
          >定时发送</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { noticeReplyLetterApi } from '@/services/api/notice';
import { form } from '@/build';
import Tinymce from '@/components/Tinymce';
import pcPreview from '../preview/pcPreview';
import mobilePreview from '../preview/mobilePreview';

export default {
  name: 'quick-reply-letter',
  components: {
    Tinymce,
    pcPreview,
    mobilePreview,
  },
  mixins: [form],
  props: ['value', 'id', 'rowData'],

  data() {
    return {
      message: {
        id: this.id,
        content: '',
        title: '',
        typeName: '',
        receiptTime: '',
        userName: '',
      },
      form: {
        id: this.id,
        content: undefined,
        sendTime: new Date(),
      },
      rules: {
        content: { required: true, message: '内容必须选择' },
      },
      preview: {
        pc: false,
        moblie: false,
      },
      isDisabled: false,
    };
  },

  computed: {},
  props: ['value', 'id', 'rowData'],
  mounted() {
    console.log(this.rowData);
    if (!_.isUndefined(this.rowData) && this.rowData.id) {
      const { receiptTime } = this.rowData;
      this.message = {
        ...this.rowData,
        receiptTime: new Date(receiptTime),
      };
    }

    this.bindSave(noticeReplyLetterApi, 'immediately', 'form', {
      beforeSend: (data) => {
        this.handleDisable();
        const { sendTime, content } = data;
        const reg = /img class="wscnph" src="api/g;
        const newContent = content.replace(reg, 'img class="wscnph" src="/api');
        return {
          ...data,
          sendTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          content: newContent,
        };
      },
      callback: ([result, data]) => {
        if (result) {
          this.$notify({
            title: '保存成功',
            type: 'success',
            duration: 2000,
          });
          this.cancel();
          this.$parent.getAll();
        } else {
          this.$notify({
            title: `保存失败.${data && data.message}`,
            type: 'error',
            duration: 2000,
          });
          this.handleDisable();
        }
      },
    });
    this.bindSave(noticeReplyLetterApi, 'delay', 'form', {
      beforeSend: (data) => {
        this.handleDisable();
        const { sendTime, content } = data;
        const reg = /img class="wscnph" src="api/g;
        const newContent = content.replace(reg, 'img class="wscnph" src="/api');
        return {
          ...data,
          sendTime: moment(sendTime).format('YYYY-MM-DD HH:mm:ss'),
          content: newContent,
        };
      },
      callback: ([result, data]) => {
        if (result) {
          this.$notify({
            title: '保存成功',
            type: 'success',
            duration: 2000,
          });
          this.cancel();
          this.$parent.getAll();
        } else {
          this.$notify({
            title: `保存失败.${data && data.message}`,
            type: 'error',
            duration: 2000,
          });
          this.handleDisable();
        }
      },
    });
  },
  methods: {
    // 关闭按钮
    cancel() {
      this.$emit('input', false);
    },
    // 是否开启和禁用发送
    handleDisable() {
      this.isDisabled = !this.isDisabled;
    },
    // 预览pc
    handlePcPreview() {
      this.preview.moblie = false;
      this.preview.pc = !this.preview.pc;
    },
    // 预览mobile
    handleMobilePreview() {
      this.preview.pc = false;
      this.preview.moblie = !this.preview.moblie;
    },
  },
};
</script>

<style lang="scss" scoped>
.createPost-container {
  padding: 30px 6%;
}
.image {
  width: 100px;
  height: 100px;
}
.el-col {
  border-radius: 4px;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.inbox-common-title {
  color: #000;
  font-weight: bold;
  margin-right: 8px;
}
</style>
